package com.bigflexlib.x.flash.events
{
	import flash.events.IEventDispatcher;
	
	
	
	/**
	 * 
	 * TODO: this class is deprecated. Use EventListenerSubscriber instead
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */	
	public class MultiEventListenerHelper
	{
		
		
		/////////////////////////////////////////////
		////////////////// target ///////////////////
		/////////////////////////////////////////////
	
		private var _target:IEventDispatcher;
		
		public function set target( v:IEventDispatcher ):void
		{
			if ( _target == v )
			{
				return;
			}
				
			if ( _target != null )	// remove previous event listeners
			{
				modifyEventListeners( _target, false );						
			}
			
			_target = v;			// update value
			
			
			if ( _target != null )	// add new event listeners
			{
				modifyEventListeners( _target, true );						
			}
			
			
		}
		
		public function get target():IEventDispatcher
		{
			return _target;
		}
		
		
		//////////////////////////////////////////////
		////////////////// listeners /////////////////
		//////////////////////////////////////////////

		private var config:Array;


		//////////////////////////////////////////////
		////////////////// constructor////////////////
		//////////////////////////////////////////////
		


		/**
		 * 
		 * 
		 * Config entries must conform to the signature of the IEventDispatcher.addEventListener() function
		 * 
		 * 
		 * @param config a two dimensional array of the form [ [ type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false  ], [ type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false  ] ... ]
		 * 
		 */		
		public function MultiEventListenerHelper( config:Array )
		{

			this.config = config;

		}
		
		
		
		
		/**
		 * 
		 * Used internally
		 * 
		 * @param dispatcher
		 * @param add
		 * 
		 */		
		private function modifyEventListeners( dispatcher:IEventDispatcher, add:Boolean ):void
		{
		
			for each ( var entry:Array in config )
			{
				var type:String = String( entry[0] );
				var listener:Function = entry[1] as Function;
				var useCapture:Boolean = entry[2] ? true : false;
				var priority:int = entry[3] ? entry[3] as int : 0;
				var useWeakReference:Boolean = entry[4] ? true : false;
			
				if ( add )
				{
					dispatcher.addEventListener( type, listener, useCapture, priority, useWeakReference );
				}
				else
				{
					dispatcher.removeEventListener( type, listener, useCapture );
				}
			
			}
		
		
		}
		
	
	
	}
	
	
}